import IconTextButton from "@/components/IconTextButton";
import React from "react";
import { View } from "react-native";
import del from "@/assets/work/del.png";
import save from "@/assets/work/save.png";
import setAvatar from "@/assets/work/setAvatar.png";
import setBg from "@/assets/work/setBg.png";
import globalStyles from "@/common/styles/globalStyles";
import * as MediaLibrary from 'expo-media-library';
import * as FileSystem from 'expo-file-system';
import Toast from "react-native-toast-message";
export interface CurrentPropsType {
    api_path:string;
    full_path:string;
    targetInd:number;
}
export default function WorkUse(props: {
	iconSize?: number;
	hideSideModal?: Function;
    currentProps:CurrentPropsType
}) {
	let { hideSideModal, iconSize = 30,  currentProps } = props;
	const workUse = (type: string) => {
		switch (type) {
			case "删除":
				console.log("删除");
				break;
			case "保存":
                saveImage()
				console.log("保存");
				break;
			case "头像":
				console.log("头像");
				break;
			case "背景":
				console.log("背景");
				break;
		}
		if (hideSideModal) {
			hideSideModal();
		}
	};
    const saveImage = async () => {
        console.log(currentProps);
        // 查看权限
        const { status } = await MediaLibrary.requestPermissionsAsync();
        if(status === 'granted'){
            const fileUri = FileSystem.documentDirectory + 'sample.jpg';
            const downloadedImage = await FileSystem.downloadAsync(currentProps.full_path, fileUri); // 将远程图片保存到临时的文件列表
            await MediaLibrary.saveToLibraryAsync(downloadedImage.uri); // 临时文件列表存放到相册
            Toast.show({
                type: 'success',
                text1:"保存成功~",
                text2:"请前往相册查看~"
            })
        }else{
            Toast.show({
                type: 'error',
                text1:"请查看权限是否打开~"
            })
        }
    }
	return (
		<View style={[globalStyles.rowLine, globalStyles.jcSB, { flex: 1 }]}>
			<IconTextButton
				onPress={() => workUse("删除")}
				iconPath={del}
				iconSize={iconSize}
				buttonText="删除"
				textColor="#000"
			/>
			<IconTextButton
				onPress={() => workUse("保存")}
				iconPath={save}
				iconSize={iconSize}
				buttonText="保存"
				textColor="#000"
			/>
			<IconTextButton
				onPress={() => workUse("头像")}
				iconPath={setAvatar}
				iconSize={iconSize}
				buttonText="头像"
				textColor="#000"
			/>
			<IconTextButton
				onPress={() => workUse("背景")}
				iconPath={setBg}
				iconSize={iconSize}
				buttonText="背景"
				textColor="#000"
			/>
		</View>
	);
}
